#!/usr/bin/lua
local gitvPath
local isWin=string.find(string.lower(os.getenv('OS') or 'nil'),'windows')~=nil
if isWin then
	gitvPath=os.getenv('HOMEDRIVE')..os.getenv('HOMEPATH').."/bin"
else
	gitvPath=os.getenv('HOME').."/bin"
end
package.path = package.path .. ";"..gitvPath.."/?.lua"
dofile(gitvPath.."/gitv") 

g_gitPaths={"~/taesooLib", "~/Documents/papers", "~/taesoo_cmu", "~/taesoo"}

if os.isFileExist(os.home_path()..'/.gitvconfig') then
	dofile(os.home_path()..'/.gitvconfig')
end

lsFiles_single=lsFiles
function lsFiles(option)
	local files={}
	for i,v in ipairs(g_gitPaths) do
		cache_git_top=v
		local out=lsFiles_single('-g')
		array.concat(files,out)
	end
	for i,v in ipairs(files) do
		local t=string.gsub(v,'~', os.home_path())
		files[i]=os.absoluteToRelativePath(t)
	end
	table.sort(files, git_tools.compareFileName)
	return files
end
lsFolders_single=lsFolders
function lsFolders(option)
	local folders={}
	for i,v in ipairs(g_gitPaths) do
		cache_git_top=v
		local out=lsFolders_single('-g', lsFiles_single)
		array.concat(folders,out)
	end
	for i,v in ipairs(folders) do
		local t=string.gsub(v,'~', os.home_path())
		folders[i]=os.absoluteToRelativePath(t)
		if folders[i] =='' then
			folders[i]='../'..os.filename(os.relativeToAbsolutePath(folders[i]))
		end
	end
	table.sort(folders, git_tools.compareFileName)
	return folders
end

if arg[1] then
	if arg[1]=='pull' or
		arg[1]=='push' or
		arg[1]=='sync' or
		arg[1]=='fetch' then
		for i,v in ipairs(g_gitPaths) do
			local t=string.gsub(v,'~', os.home_path())
			cache_git_top=t
			print('\n> cd "'..v..'"')
			main(unpack(arg))
		end
	elseif arg[1]=='diff' or 
		arg[1]=='ci' or
		arg[1]=='cif' or
		arg[1]=='ts' then
		main('all', unpack(arg))
	else
		main(unpack(arg))
	end
else
	printHelp()
end
